﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using DevelopStudio.Basic;
using DevelopStudio.Control.Basic;

namespace DevelopStudio.Control
{
    public partial class GeneralPageControl : PageControl
    {
        private List<ChangelistInfo> removedChangelist = new List<ChangelistInfo>();

        public GeneralPageControl()
        {
            InitializeComponent();
        }

        public override void InitializePageControl()
        {
            base.InitializePageControl();
        }

        public override bool ValidatePage()
        {
            return base.ValidatePage();
        }

        public override void ApplyChanges()
        {
            base.ApplyChanges();

            List<ChangelistInfo> changelistInfos = new List<ChangelistInfo>();

            foreach (ListViewItem var in this.listViewShow.Items)
            {
                ChangelistInfo listInfo = var.Tag as ChangelistInfo;

                if (listInfo == null)
                {
                    continue;
                }

                listInfo.Displayed = true;
            }

            foreach (ListViewItem var in this.listViewHide.Items)
            {
                ChangelistInfo listInfo = var.Tag as ChangelistInfo;

                if (listInfo == null)
                {
                    continue;
                }

                listInfo.Displayed = false;
            }

            foreach (ChangelistInfo listInfo in this.removedChangelist)
            {
                WorkingCopyInfo copyInfo = PageData.WorkingCopies.Find(o => o.Path == listInfo.WorkingCopyPath);

                if (copyInfo == null)
                {
                    continue;
                }

                copyInfo.ChangelistInfos.Remove(listInfo);
            }
        }

        private void GeneralPageControl_Load(object sender, EventArgs e)
        {
            foreach (WorkingCopyInfo copyInfo in PageData.WorkingCopies)
            {
                foreach (ChangelistInfo listInfo in copyInfo.ChangelistInfos)
                {
                    ListViewItem item = new ListViewItem(new string[] { listInfo.Name, copyInfo.Path }) { Tag = listInfo };

                    if (listInfo.Status != (int)AppEnum.ChangelistStatus.Disabled)
                    {
                        if (listInfo.Displayed == true)
                        {
                            this.listViewShow.Items.Add(item);
                        }
                        else
                        {
                            this.listViewHide.Items.Add(item);
                        }
                    }
                    else
                    {
                        if (listInfo.WorkingCopyPath != copyInfo.Path)
                        {
                            listInfo.WorkingCopyPath = copyInfo.Path;
                        }

                        this.listViewDisappear.Items.Add(item);
                    }
                }
            }
        }

        private void buttonLeft_Click(object sender, EventArgs e)
        {
            this.MoveToLeft();
        }

        private void buttonRight_Click(object sender, EventArgs e)
        {
            this.MoveToRight();
        }

        private void listViewShow_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.MoveToRight();
        }

        private void MoveToRight()
        {
            ListViewItem[] selectItems = new ListViewItem[this.listViewShow.SelectedItems.Count];
            this.listViewShow.SelectedItems.CopyTo(selectItems, 0);

            foreach (ListViewItem var in selectItems)
            {
                this.listViewShow.Items.Remove(var);
                this.listViewHide.Items.Add(var);
            }
        }

        public void MoveToLeft()
        {
            ListViewItem[] selectItems = new ListViewItem[this.listViewHide.SelectedItems.Count];
            this.listViewHide.SelectedItems.CopyTo(selectItems, 0);

            foreach (ListViewItem var in selectItems)
            {
                this.listViewHide.Items.Remove(var);
                this.listViewShow.Items.Add(var);
            }
        }

        private void checkBoxSelectAll_CheckedChanged(object sender, EventArgs e)
        {
            foreach (ListViewItem var in this.listViewDisappear.Items)
            {
                var.Checked = this.checkBoxSelectAll.Checked;
            }
        }

        private void buttonRemoveDisappear_Click(object sender, EventArgs e)
        {
            ListViewItem[] selectItems = new ListViewItem[this.listViewDisappear.CheckedItems.Count];
            this.listViewDisappear.CheckedItems.CopyTo(selectItems, 0);

            foreach (ListViewItem var in selectItems)
            {
                this.listViewDisappear.Items.Remove(var);

                ChangelistInfo changelist = var.Tag as ChangelistInfo;

                if (changelist != null)
                {
                    this.removedChangelist.Add(changelist);
                }
            }
        }

        private void listViewHide_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.MoveToLeft();
        }
    }
}
